<?php
/**
 * This file contain StatEntries and StatsBrowsing classes.
 * @package stats
 * @author Mundusnovis studio
 * @version 1.0
 * @copyright 2007 Mundusnovis studio
 * @site http://mundusnovis.info
 */
 	/**
	 * Communication with database provide saving and loading statistics of CMS
	 * @package stats
	 * @author Mundusnovis studio
     * @version 1.0
     * @copyright 2007 Mundusnovis studio
	 */
	class StatsEntries {
		/**
		 * Curent date-time
		 * @access private
		 * @var int
		 */
		private $date;
		/**
		 * IP of client
		 * @access private
		 * @var string
		 */
		private $ip;
		/**
		 * User agent what user using
		 * @access private
		 * @var string
		 */
		private $usr_agent;

		/**
		 * Constructor. Do all work with saving stats to database
		 *
		 * TODO: Comment code little
		 * @access public
		 * @return int
		 */
		function __construct() {
			global $mae, $db;
			$this->date = date('Y-m-d H:i:s');
			$this->ip = $_SERVER['REMOTE_ADDR'];
			$this->usr_agent = $_SERVER['HTTP_USER_AGENT'];
			if (!empty($_COOKIE['lastEntry'])) $unique = '0'; else {
				$db->modeNum();
				$ans = $db->query("SELECT COUNT(*) FROM `".$mae['db_prefix']."stats_entries` WHERE `ip` = '".ip2long($this->ip)."';");
				$unique = ($ans->fields[0] == '0') ? '1' : '0';
			}
			$ans = $db->query("INSERT INTO `".$mae['db_prefix']."stats_entries` (`date`, `ip`, `usr_agent`, `unique`) VALUES (NOW(), ".ip2long($this->ip).", '".$this->usr_agent."', '".$unique."');");
			setcookie('lastEntry', date('Y-m-d H:i:s'), time()+3600*48*365);
			$_SESSION['entry'] = ($ans) ? true : false;
		}
	}

	/**
	 * Permits to browse statistics. All function all static.
	 * @package stats
	 * @author Mundusnovis studio
	 * @version 1.0
	 * @copyright 2007 Mundusnovis studio
	 */
	class StatsBrowsing {
		static function months($date = '') {
			global $mae, $db;
			$date = ($date == '') ? date('Y') : $date;
			$db->modeAssoc();
			$res = $db->query("SELECT DATE_FORMAT(date, '%c') AS `month`, COUNT(*) AS `value` FROM `".$mae['db_prefix']."stats_entries` WHERE `unique` = 0 AND DATE_FORMAT(date, '%Y') = '".$date."' GROUP BY DATE_FORMAT(date, '%Y-%m');");
			for ($i=1; $i<=12; $i++) $arr1[$i] = '0';
			foreach($res as $assoc) $arr1[$assoc['month']] = $assoc['value'];
			$res = $db->query("SELECT DATE_FORMAT(date, '%c') AS `month`, COUNT(*) AS `value` FROM `".$mae['db_prefix']."stats_entries` WHERE `unique` = 1 AND DATE_FORMAT(date, '%Y') = '".$date."' GROUP BY DATE_FORMAT(date, '%Y-%m');");
			for ($i=1; $i<=12; $i++) $arr2[$i] = '0';
			foreach($res as $assoc) $arr2[$assoc['month']] = $assoc['value'];
			for ($i=1; $i<=12; $i++) $arr[$i] = array($arr2[$i], $arr1[$i]);
			return $arr;
		}
		static function days($date = '') {
			global $mae, $db;
			$date = ($date == '') ? date('Y-m') : $date;
			$db->modeAssoc();
			$res = $db->query("SELECT DATE_FORMAT(date, '%e') AS `day`, COUNT(*) AS `value` FROM `".$mae['db_prefix']."stats_entries` WHERE `unique` = 0 AND DATE_FORMAT(date, '%Y-%m') = '".$date."' GROUP BY DATE_FORMAT(date, '%Y-%m-%d');");
			$numberOfDays = date('t', strtotime($dat.'-01'));
			for ($i=1; $i<=$numberOfDays; $i++) $arr1[$i] = '0';
			foreach($res as $assoc) $arr1[$assoc['day']] = $assoc['value'];
			$res = $db->query("SELECT DATE_FORMAT(date, '%e') AS `day`, COUNT(*) AS `value` FROM `".$mae['db_prefix']."stats_entries` WHERE `unique` = 1 AND DATE_FORMAT(date, '%Y-%m') = '".$date."' GROUP BY DATE_FORMAT(date, '%Y-%m-%d');");
			for ($i=1; $i<=$numberOfDays; $i++) $arr2[$i] = '0';
			foreach($res as $assoc) $arr2[$assoc['day']] = $assoc['value'];
			for ($i=1; $i<=$numberOfDays; $i++) $arr[$i] = array($arr2[$i], $arr1[$i]);
			return $arr;
		}
		static function browsers() {
			require_once('internal/Browscap.php');
			$bc = new Browscap('internal/browscap/');
			$bc->doAutoUpdate = false;
			$bc->updateMethod = 'local';
			global $mae, $db;
			$db->modeAssoc();
			$res = $db->query("SELECT `usr_agent` FROM `".$mae['db_prefix']."stats_entries`;");
			foreach($res as $assoc) {
				$pgb = $bc->getBrowser($assoc['usr_agent'], true);
				if (!isset($arr[$pgb['Browser']])) $arr[$pgb['Browser']]=1;
				else $arr[$pgb['Browser']]++;
			}
			arsort($arr);
			return $arr;
		}
		static function platforms() {
			require_once('internal/Browscap.php');
			$bc = new Browscap('internal/browscap/');
			$bc->doAutoUpdate = false;
			$bc->updateMethod = 'local';
			global $mae, $db;
			$db->modeAssoc();
			$res = $db->query("SELECT `usr_agent` FROM `".$mae['db_prefix']."stats_entries`;");
			foreach($res as $assoc) {
				$pgb = $bc->getBrowser($assoc['usr_agent'], true);
				if (!isset($arr[$pgb['Platform']])) $arr[$pgb['Platform']]=1;
				else $arr[$pgb['Platform']]++;
			}
			arsort($arr);
			return $arr;
		}
	}
?>
